﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameView.Comment
{
    public class NotificationService : INotificationService
    {
        public ISnackbarMessageQueue MessageQueue { get=>_messageQueue; }
        private readonly ISnackbarMessageQueue _messageQueue;
        private readonly TimeSpan _defaultDuration = TimeSpan.FromSeconds(3);
        private readonly TimeSpan _errorDuration = TimeSpan.FromSeconds(5);

        public NotificationService(ISnackbarMessageQueue messageQueue)
        {
            _messageQueue = messageQueue;
        }

        public void Show(string message, NotificationType type = NotificationType.Information)
        {
            Show(new NotificationMessage
            {
                Content = message,
                Type = type,
                Duration = GetDurationByType(type)
            });
        }

        public void Show(string message, string actionText, Action action, NotificationType type = NotificationType.Information)
        {
            Show(new NotificationMessage
            {
                Content = message,
                ActionText = actionText,
                Action = action,
                Type = type,
                Duration = GetDurationByType(type)
            });
        }

        public void Show(NotificationMessage message)
        {
            var duration = message.Duration ?? GetDurationByType(message.Type);
            var icon = GetIconByType(message.Type);
             
                _messageQueue.Enqueue(new SnackbarMessage
                {
                    Content = message.Content,
                    ActionContent = message.ActionText,

                });
            
        }

        public void ShowSuccess(string message)
        {
            Show(message, NotificationType.Success);
        }

        public void ShowError(string message)
        {
            Show(message, NotificationType.Error);
        }

        public void ShowWarning(string message)
        {
            Show(message, NotificationType.Warning);
        }

        public void ShowInfo(string message)
        {
            Show(message, NotificationType.Information);
        }

        public void ShowSuccess(string message, string actionText, Action action)
        {
            Show(message, actionText, action, NotificationType.Success);
        }

        public void ShowError(string message, string actionText, Action action)
        {
            Show(message, actionText, action, NotificationType.Error);
        }

        private TimeSpan GetDurationByType(NotificationType type)
        {
            return type == NotificationType.Error ? _errorDuration : _defaultDuration;
        }

        private PackIcon GetIconByType(NotificationType type)
        {
            return type switch
            {
                NotificationType.Success => new PackIcon { Kind = PackIconKind.CheckCircle },
                NotificationType.Error => new PackIcon { Kind = PackIconKind.AlertCircle },
                NotificationType.Warning => new PackIcon { Kind = PackIconKind.Alert },
                NotificationType.Information => new PackIcon { Kind = PackIconKind.Information },
                _ => null
            };
        }
    }
}
